package com.hyjx.framework.service;

import com.hyjx.framework.common.Constants;
import com.hyjx.framework.common.util.SpringBeanUtil;
import com.hyjx.framework.service.model.CacheUser;
import com.hyjx.framework.uc.depart.DepartModel;
import com.hyjx.framework.uc.depart.DepartService;
import com.hyjx.framework.uc.user.UserInfo;
import com.hyjx.framework.uc.user.UserModel;
import com.hyjx.framework.uc.user.UserService;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息服务类
 *
 * @author dijian
 */
@SuppressWarnings("unused")
public class UserCenterService {
    private static UserService userService;
    private static DepartService departService;
    
    static {
        CacheService.set("catchMap", new HashMap<String, Object>());
    }
    
    /**
     * 获得当前登录用户信息，包括用户基本信息、角色信息、菜单信息
     *
     * @return
     */
    public static UserInfo getCurrentUserInfo() {
        return (UserInfo) SessionManager.get(Constants.USERINFO);
    }
    
    /**
     * 取当前用户相关信息
     */
    public static CacheUser getCurrUserRelaInfo() {
        return RightManager.getCurrUserRelaInfoById(getCurrentUserId());
    }
    
    /**
     * 获得当前登录用户信息，包括用户基本信息、角色信息、菜单信息
     *
     * @return
     */
    public static String getCurrentUserId() {
        UserInfo userInfo = (UserInfo) SessionManager.get(Constants.USERINFO);
        return userInfo.getUser_id();
    }
    
    public static String getCurrentUserOrgLevelId() {
        return RightManager.getSysOrgs().get(getCurrentUserInfo().getOrg_code()).getOrg().getLevelid();
    }
    
    public static String getCurrentUserDataOrgLevelId() {
        return RightManager.getSysOrgs().get(getCurrentUserInfo().getDataOrgCode()).getOrg().getLevelid();
    }
    
    /**
     * 通过部门id，获得制定部门下的所有用户
     *
     * @param depart_Code
     */
    @SuppressWarnings("unchecked")
    public static List<UserModel> getUsersByDeptCode(String depart_Code) {
        List<UserModel> list = null;
        Map<String, Object> catchMap = (Map<String, Object>) CacheService.get("catchMap");
        list = (List<UserModel>) catchMap.get("getUsersByDept" + depart_Code);
        if (list == null) {
            userService = (UserService) SpringBeanUtil.getBean("userServiceImpl");
            try {
                list = userService.getUsersByDeptCode(depart_Code);
                catchMap.put("getUsersByDept" + depart_Code, list);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    
    /**
     * 通过用户id，获得用户的基本信息
     *
     * @param user_id
     */
    @SuppressWarnings("unchecked")
    public static UserModel getUserByID(String user_id) {
        UserModel userModel = null;
        Map<String, Object> catchMap = (Map<String, Object>) CacheService.get("catchMap");
        userModel = (UserModel) catchMap.get("getUserByID" + user_id);
        if (userModel == null) {
            userService = (UserService) SpringBeanUtil.getBean("userServiceImpl");
            userModel = userService.getUserById(user_id);
            catchMap.put("getUserByID" + user_id, userModel);
        }
        return userModel;
    }
    
    /**
     * 通过部门id，获得部门的基本信息
     *
     * @param depart_id
     * @return DepartModel
     */
    @SuppressWarnings("unchecked")
    public static DepartModel getDepartByID(String depart_id) {
        DepartModel departModel = null;
        Map<String, Object> catchMap = (Map<String, Object>) CacheService.get("catchMap");
        departModel = (DepartModel) catchMap.get("getDepartByID" + depart_id);
        if (departModel == null) {
            departService = (DepartService) SpringBeanUtil.getBean(Constants.DEPART_SERVICE_NAME);
            departModel = departService.getDepartById(depart_id);
            catchMap.put("getDepartByID" + depart_id, departModel);
        }
        return departModel;
    }
    
    /**
     * 通过单位id，获得单位下的子单位和部门信息
     *
     * @param unit_id
     * @return List<DepartModel>
     */
    @SuppressWarnings("unchecked")
    public static List<DepartModel> getDepartsByUnitID(String unit_id) {
        List<DepartModel> list = null;
        Map<String, Object> catchMap = (Map<String, Object>) CacheService.get("catchMap");
        list = (List<DepartModel>) catchMap.get("getDepartsByUnitID" + unit_id);
        if (list == null) {
            departService = (DepartService) SpringBeanUtil.getBean(Constants.DEPART_SERVICE_NAME);
            DepartModel model = new DepartModel();
            // model.setDepart_id(unit_id);
            list = departService.getDepartModelList(model);
            catchMap.put("getDepartsByUnitID" + unit_id, list);
        }
        return list;
    }
    
    /**
     * 获取用户所有部门权限
     *
     * @return 返回depart_code以英文逗号隔开
     */
    public static String getRoleDepart() {
        List<DepartModel> departlist = getCurrentUserInfo().getDepartlist();
        String depart_codes = "";
        if (departlist != null) {
            for (DepartModel model : departlist) {
                depart_codes += model.getDepart_code() + ",";
            }
        }
        if (depart_codes.length() > 1)
            depart_codes = depart_codes.substring(0, depart_codes.length() - 1);
        return depart_codes;
    }
    
    /**
     * 当前用户所在单位市局代码
     *
     * @return
     */
    public static String getDepart_city() {
        String cityCode = null;
        cityCode = SessionManager.getString("cityCode");
        if (cityCode == null) {
            departService = (DepartService) SpringBeanUtil.getBean(Constants.DEPART_SERVICE_NAME);
            DepartModel model = new DepartModel();
            model.setDepart_code_full(getCurrentUserInfo().getDepart().getDepart_code_full());
            model.setDepart_level("3");
            cityCode = departService.getDepart_city(model);
            SessionManager.set("cityCode", cityCode);
        }
        return cityCode;
    }
    
    /**
     * 当前用户所在单位区县局代码
     *
     * @return
     */
    public static String getDepart_county() {
        String DepartCountyCode = null;
        DepartCountyCode = SessionManager.getString("DepartCountyCode");
        if (DepartCountyCode == null) {
            departService = (DepartService) SpringBeanUtil.getBean(Constants.DEPART_SERVICE_NAME);
            DepartModel model = new DepartModel();
            model.setDepart_code_full(getCurrentUserInfo().getDepart().getDepart_code_full());
            model.setDepart_level("4");
            DepartCountyCode = departService.getDepart_city(model);
            SessionManager.set("DepartCountyCode", DepartCountyCode);
        }
        return DepartCountyCode;
    }
    
    /**
     * 当前用户所在单位区工商所代码
     *
     * @return
     */
    public static String getDepart_localadm() {
        String departLocaladmCode = null;
        departLocaladmCode = SessionManager.getString("departLocaladmCode");
        if (departLocaladmCode == null) {
            departService = (DepartService) SpringBeanUtil.getBean(Constants.DEPART_SERVICE_NAME);
            DepartModel model = new DepartModel();
            model.setDepart_code_full(getCurrentUserInfo().getDepart().getDepart_code_full());
            model.setDepart_level("5");
            departLocaladmCode = departService.getDepart_city(model);
            SessionManager.set("departLocaladmCode", departLocaladmCode);
        }
        return departLocaladmCode;
    }
    
    /**
     * 根据部门编码取级次
     *
     * @param code
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String getDepartLevelByCode(String code) {
        String departLevel = "";
        DepartModel departModel = null;
        Map<String, Object> catchMap = (Map<String, Object>) CacheService.get("catchMap");
        departLevel = (String) catchMap.get("DepartLevel" + code);
        if (departLevel == null) {
            departService = (DepartService) SpringBeanUtil.getBean(Constants.DEPART_SERVICE_NAME);
            departModel = departService.getDepartByCode(code);
            if (departModel != null) {
                departLevel = departModel.getDepart_level();
            }
            catchMap.put("DepartLevel" + code, departLevel);
        }
        return departLevel;
    }
    
    /**
     * 取当前用户的单位编码
     *
     * @return
     */
    public static String getCurrentUserOrgCode() {
        return getCurrentUserInfo().getOrg_code();
    }
    
    /**
     * 取当前用户部门
     *
     * @return
     */
    public static String getCurrentUserDepartCode() {
        return getCurrentUserInfo().getDepart_code();
    }
    
    /**
     * 取当前 用户的子系统 id
     *
     * @return
     */
    public static String getCurrentSubsys_id() {
        return SessionManager.getString("subsys_id");
    }
}